package com.example.day19;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class MyMusicService extends Service {
    final int CANT_PLAY=0;
    final int CAN_PLAY=1;
    final int PLAYING=2;
    final int PAUSE=3;
    final int CHANGE=4;
    int playTime = 0;
    int isCanStart = CANT_PLAY;//播放状态0：未准备完成 1准备完成 2播放中 3暂停 4换曲
    musicData nowMusic;

    MediaPlayer mp;
    private ArrayList<musicData> list=new ArrayList<>();
    public MyMusicService() {
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        Log.e("#########","绑定成功");
        IntentFilter filter = new IntentFilter();
        filter.addAction("music");
       return new MyBinder();
    }



    public class MyBinder extends Binder{
        public void playMusic(){
            Log.e("#####"+isCanStart,"@@@@@@@@@@@");
            if (isCanStart==CANT_PLAY) {
                Toast.makeText(getBaseContext(), "音频文件未准备好", Toast.LENGTH_SHORT).show();
                return;
            }
            if (!mp.isPlaying()) {
                mp.seekTo(playTime);
                mp.start();
            } else {
                isCanStart=PAUSE;
                playTime=mp.getCurrentPosition();
                mp.pause();
            }
        }
        public void stopMusic(){
            mp.stop();
            mp.reset();
            playTime=0;
            isCanStart=CANT_PLAY;
           initPlayer(nowMusic);


        }
        public void pause(){
            playTime=mp.getCurrentPosition();
            mp.pause();
        }
        public boolean initPlayer(musicData data) {
            nowMusic=data;

            if(isCanStart!=CHANGE){
                isCanStart = CANT_PLAY;
            }
            Log.e("ssss#####"+isCanStart,data.getTitle()+"");
            String filename = data.getData();
            File file = new File(filename);
            Log.e("ssss#####"+isCanStart,data.getTitle()+"@@@"+file.getPath());
            try {
                if(mp==null){
                    mp=new MediaPlayer();
                }
                mp.setDataSource(file.getPath());
                Log.e("播放器", file.toString());
                mp.prepareAsync();
                mp.setOnPreparedListener(new mpListener());
                return true;
            } catch (Exception e) {
                e.printStackTrace();
                Log.e("播放器Exception", e.toString());
            }
            return false;
        }

        //TODO 播放下一收
        public void next(musicData data){
            //停止当前的歌
            mp.stop();
            mp.reset();
            playTime=0;
            isCanStart=CHANGE;
            initPlayer(data);

//            //新的歌曲
//            int num=(Math.abs(++current))%list.size();
//            mediaPlayer=MediaPlayer.create(this,(list.get(num)));
//            mediaPlayer.start();
        }
        //TODO 播放上 一首
        public void last(musicData data){
            mp.stop();
            mp.reset();
            playTime=0;
            isCanStart=CHANGE;
            initPlayer(data);
            //停止当前的歌
//            stop();
//            //新的歌曲
//            int num=(Math.abs(--current))%list.size();
//            mediaPlayer=MediaPlayer.create(this,(list.get(num)));
//            mediaPlayer.start();
        }

        //TODO 返回当前进度
        public int getCurrent(){
            if(mp!=null){
                int positon = mp.getCurrentPosition();//当前播放的时长
                int duration = mp.getDuration();//总时长
                return positon*100/duration;
            }
            return 0;
        }
        //TODO 设置进度
        public  void setPositon(int progress){//50
            if(mp!=null)
                mp.seekTo(mp.getDuration()*progress/100);

        }

    }

    private class mpListener implements MediaPlayer.OnPreparedListener {

        @Override
        public void onPrepared(MediaPlayer mp) {
            if(isCanStart==CHANGE){
                mp.seekTo(playTime);
                mp.start();
            }else {
                isCanStart = CAN_PLAY;
            }
        }
    }
}
